package com.itheima.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.aspect.annotation.OperaLog;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 员工管理
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session){
        Map resultLogin = service.checkLogin(employee,session);
        Object emp = resultLogin.get("emp");
        if (emp != null){
            return R.success(emp);
        }else {
            return R.error((String) resultLogin.get("msg"));
        }
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success(null);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R addEmployee(@RequestBody Employee employee){
        int count = service.add(employee);
        return R.success(count);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @OperaLog(value = "分页查询",username = "admin")
    @GetMapping("/page")
    public R pageSelect( Integer page, Integer pageSize, String name){
        Page pageResult = service.pageSelect(page,pageSize,name);
        return R.success(pageResult);
    }

    /**
     * 禁用启用
     * @param employee
     * @return
     */
    @PutMapping
    public R enableOrDisable(@RequestBody Employee employee){
        service.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 修改员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R editEmployee(@PathVariable Long id){
        Employee byId = service.getById(id);
        log.info(byId.toString());
        return R.success(byId);
    }
}
